package com.project.common.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class PageTag extends TagSupport{

	private static final long serialVersionUID = 1L;

	private int pageNo;//当前页数
	
	private int totalCount;//数据总条数
	
	private int pageSize;//每页多少条数据
	
	private String formId;
	
	@Override
	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		JspWriter out = this.pageContext.getOut();
		//计算总页数
		int totalPage = totalCount/pageSize;
		if(totalCount%pageSize!=0){
			totalPage++;
		}
		int minPage = 1;//最小页数
		if(pageNo-3>=1){
			minPage = pageNo-3;
		}
		int maxPage = pageNo+3;//最大页数
		if(maxPage>=totalPage){
			maxPage = totalPage;
		}
		try {
			out.print("<div class='project-pagebox'>");
			out.print("<a href='javascript:jumpPage("+1+",\""+formId+"\")'>首页</a>");
			if(pageNo>1){	
				out.print("<a href='javascript:jumpPage("+(pageNo-1)+",\""+formId+"\")'><</a> ");
			}
			while (minPage<=maxPage) {
				if(minPage==pageNo){
					out.print("<a href='javascript:jumpPage("+minPage+",\""+formId+"\")' class='active'>"+minPage+"</a>");
				}else{
					out.print("<a href='javascript:jumpPage("+minPage+",\""+formId+"\")'>"+minPage+"</a>");
				}
				minPage++;
			}
			if(maxPage!=totalPage){
				out.print("……<a href='javascript:jumpPage("+totalPage+",\""+formId+"\")'>"+totalPage+"</a>");
			}
			if(pageNo<maxPage){				
				out.print("<a href='javascript:jumpPage("+(pageNo+1)+",\""+formId+"\")'>></a> ");
			}
			out.append("<a href='javascript:jumpPage("+totalPage+",\""+formId+"\")'>末页</a>");
			out.print("</div>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	
	@Override

    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getFormId() {
		return formId;
	}

	public void setFormId(String formId) {
		this.formId = formId;
	}
}
